package com.rpframework.module.common.bottom.controller;


import com.github.pagehelper.PageInfo;
import com.rpframework.core.json.FailException;
import com.rpframework.core.json.JsonResp;
import com.rpframework.core.json.ParameterException;
import com.rpframework.core.mybatis.mapper.Paging;
import com.rpframework.core.utils.Md5Utils;
import com.rpframework.module.common.bottom.entity.AdminUser;
import com.rpframework.module.common.bottom.entity.enums.SourceType;
import com.rpframework.module.common.bottom.service.AdminUserService;
import com.rpframework.module.common.domain.UserLogin;
import com.rpframework.module.common.service.UserTokenService;
import com.rpframework.module.common.url.RequestDescription;
import com.rpframework.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@ResponseBody
@RequestMapping
@RequestDescription("用户相关")
@Controller
public class AdminUserController extends BaseController {

    private final AdminUserService adminUserService;
    private final UserTokenService userTokenService;

    @Autowired
    public AdminUserController(UserTokenService uerTokenService, UserTokenService userTokenService, AdminUserService adminUserService) {
        super(uerTokenService);
        this.userTokenService = userTokenService;
        this.adminUserService = adminUserService;
    }

    @RequestMapping
    @RequestDescription("用户详情")
    public JsonResp<AdminUser> detail() {
        return JsonResp.ok(adminUserService.selectByPrimaryKey(getUserIdAdmin()));
    }

    /**
     * @param user 用户登陆信息
     */
    @RequestMapping
    @RequestDescription("登陆[用户名/密码]")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp login(HttpServletResponse response, AdminUser user) {
        if (user == null || StringUtil.isBlank(user.getName(), user.getPassword())) {
            throw new ParameterException("用户名或密码不能为空");
        }
        user.setPassword(Md5Utils.md5(user.getPassword()));
        AdminUser adminUser = adminUserService.selectOne(user);
        if (adminUser == null) {
            throw new ParameterException("用户名或密码不正确");
        }
        SourceType admin = SourceType.ADMIN;
        String token = userTokenService.addToken(new UserLogin(adminUser.getId().toString(), admin));
        if (StringUtils.isBlank(token)) {
            throw new FailException();
        }
        //添加COOKIE
        SourceType.addLoginCookie(admin, response, token);

        return new JsonResp().succ("登陆成功", token);
    }

    /**
     * 查询用户
     */
    @RequestMapping
    @RequestDescription("查询商品订单：userId")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp info(Paging paging) {
        JsonResp respJson = new JsonResp();
        Example example = adminUserService.getExample();
        example.orderBy("gmtDatetime").desc();
        PageInfo<AdminUser> selectPage = adminUserService.selectPage(example, paging);
        //查询商品
        return respJson.succ(selectPage);
    }


    @RequestMapping
    @RequestDescription("删除")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp delete(Long id) {
        JsonResp respJson = new JsonResp();
        if (null == id) {
            throw new ParameterException("获取删除对象失败");
        }
        int deleteByPrimaryKey = adminUserService.deleteByPrimaryKey(id);
        if (deleteByPrimaryKey <= 0) {
            throw new ParameterException("删除失败");
        }
        //查询商品
        return respJson.succ("成功");
    }


    @RequestMapping
    @RequestDescription("添加用户")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp insert(String name, String password, Integer type) {
        JsonResp respJson = new JsonResp();
        if (null == type) {
            type = 0;
        }
        if (StringUtils.isBlank(name)) {
            throw new ParameterException("请输入账号");
        }
        if (StringUtils.isBlank(password)) {
            throw new ParameterException("请输入密码");
        }

        AdminUser adminUser = new AdminUser();
        adminUser.setName(name);
        List<AdminUser> select = adminUserService.select(adminUser);
        if (select.size() > 0) {
            throw new ParameterException("已存在该用户");
        }
        adminUser.setPassword(Md5Utils.md5(password));
        int insert = adminUserService.insert(adminUser);
        if (insert <= 0) {
            throw new ParameterException("添加失败");
        }
        //查询商品
        return respJson.succ("成功");
    }


    @RequestMapping
    @RequestDescription("修改密码")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp updatePassword(HttpServletRequest request, String password) {
        JsonResp respJson = new JsonResp();
        if (StringUtils.isBlank(password)) {
            throw new ParameterException("请输入密码");
        }
        Long userIdAdmin = getUserIdAdmin(request);
        AdminUser adminUser = new AdminUser();
        adminUser.setId(userIdAdmin);
        adminUser.setPassword(Md5Utils.md5(password));
        int updateByPrimaryKeySelective = adminUserService.updateByPrimaryKeySelective(adminUser);
        if (updateByPrimaryKeySelective <= 0) {
            throw new ParameterException("修改失败");
        }
        return respJson.succ("成功");
    }
}
